package by.alex.dzeshko.facebooksuperclient.database;

import org.json.JSONObject;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.api.ApiRequest;
import by.alex.dzeshko.facebooksuperclient.models.FbNewsFeed;
import by.alex.dzeshko.facebooksuperclient.providers.contract.FbContract;

public class DbManager extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 17;

	private static final String DATABASE_NAME = "fbsuperclient.store.db";

	public static final String FEED_TABLE = "FEED";

	public static final String FRIENDS_TABLE = "FRIENDS";
	
	public static final String USER_PROFILE_TABLE = "USER_PROFILE";
		
	//FEED_COLUMNS
	public static final int ID_COL_INDEX = 0;

	public static final int FROM_NAME_COL_INDEX = 1;

	public static final int FROM_ID_COL_INDEX = 2;
	
	public static final int TYPE_COL_INDEX = 3;

	public static final int CREATED_TIME_COL_INDEX = 4;

	public static final int MESSAGE_COL_INDEX = 5;

	public static final int LIKES_COUNT_COL_INDEX = 6;

	public static final int STORY_COL_INDEX = 7;

	public static final int CAPTION_COL_INDEX = 8;

	public static final int CONTENT_PICTURE_COL_INDEX = 9;

	public static final int COMMENT_LINK_COL_INDEX = 10;

	public static final int LIKE_LINK_COL_INDEX = 11;

	public static final int COMMENTS_COUNT_COL_INDEX = 12;
	
	public static final int FEED_USER_PICTURE_URL_COL_INDEX = 13;
	
	public static final int TABLE_TYPE_COL_INDEX = 14;

	 //FRIEND_COLUMNS
	public static final int FRIEND_NAME_COL_INDEX = 1;

	public static final int FRIEND_PICTURE_URL_COL_INDEX = 2;

	// USER_PROFILE_COLUMNS
	public static final int NAME_COL_INDEX = 1;

	public static final int USER_NAME_COL_INDEX = 2;

	public static final int BIRTHDAY_COL_INDEX = 3;

	public static final int HOMETOWN_COL_INDEX = 4;

	public static final int LOCATION_COL_INDEX = 5;

	public static final int GENDER_COL_INDEX = 6;

	public static final int RELATIONSHIP_STATUS_COL_INDEX = 7;

	public static final int RELIGION_COL_INDEX = 8;

	public static final int POLITICAL_COL_INDEX = 9;
	
	public static final int PICTURE_URL_COL_INDEX = 10;


	// COLUMNS
	public static final String[] FEED_COLUMNS = {FbContract.FeedColumns.ITEM_ID, 
			FbContract.FeedColumns.FROM_NAME, FbContract.FeedColumns.FROM_ID,
			FbContract.FeedColumns.NEWS_TYPE, FbContract.FeedColumns.CREATED_TIME, 
			FbContract.FeedColumns.MESSAGE, FbContract.FeedColumns.LIKES_COUNT,
			FbContract.FeedColumns.STORY, FbContract.FeedColumns.CAPTION,
			FbContract.FeedColumns.CONTENT_PICTURE, FbContract.FeedColumns.COMMENT_LINK,
			FbContract.FeedColumns.LIKE_LINK, FbContract.FeedColumns.COMMENTS_COUNT,
			FbContract.FeedColumns.USER_PICTURE_URL, FbContract.FeedColumns.TABLE_TYPE};

	public static final String[] FRIENDS_COLUMNS = {
			FbContract.FeedColumns._ID, FbContract.FriendsColumns.FRIEND_NAME, FbContract.FriendsColumns.PICTURE_URL};

	public static final String[] USER_PROFILE_COLUMNS = {
		FbContract.UserProfileColumns.ITEM_ID, FbContract.UserProfileColumns.NAME,
		FbContract.UserProfileColumns.USER_NAME, FbContract.UserProfileColumns.BIRTHDAY,
		FbContract.UserProfileColumns.HOMETOWN, FbContract.UserProfileColumns.LOCATION,
		FbContract.UserProfileColumns.GENDER, //FbContract.UserProfileColumns.INTERESTED_IN,
		FbContract.UserProfileColumns.RELATIONSHIP_STATUS, FbContract.UserProfileColumns.RELIGION,
		FbContract.UserProfileColumns.POLITICAL, FbContract.UserProfileColumns.PICTURE_URL,};
	
	// CREATE TABLE
	public static final String CREATE_FEED_TABLE_SQL = "CREATE TABLE "
			+ FEED_TABLE + " (" 
			+ FEED_COLUMNS[ID_COL_INDEX] 				+ " INTEGER PRIMARY KEY, " 
			+ FEED_COLUMNS[FROM_NAME_COL_INDEX]			+ " VARCHAR NOT NULL, " 
			+ FEED_COLUMNS[FROM_ID_COL_INDEX] 			+ " VARCHAR NOT NULL, "
			+ FEED_COLUMNS[TYPE_COL_INDEX]				+ " VARCHAR NOT NULL, " 
			+ FEED_COLUMNS[CREATED_TIME_COL_INDEX]		+ " VARCHAR NOT NULL, "
			+ FEED_COLUMNS[TABLE_TYPE_COL_INDEX]		+ " VARCHAR NOT NULL, " 
			+ FEED_COLUMNS[COMMENT_LINK_COL_INDEX]		+ " INTEGER, " 
			+ FEED_COLUMNS[LIKE_LINK_COL_INDEX]			+ " INTEGER, " 
			+ FEED_COLUMNS[MESSAGE_COL_INDEX] 			+ " VARCHAR, " 
			+ FEED_COLUMNS[LIKES_COUNT_COL_INDEX]		+ " INTEGER, " 
			+ FEED_COLUMNS[COMMENTS_COUNT_COL_INDEX]	+ " INTEGER, " 
			+ FEED_COLUMNS[STORY_COL_INDEX]				+ " VARCHAR, " 
			+ FEED_COLUMNS[CAPTION_COL_INDEX]			+ " VARCHAR, "
			+ FEED_COLUMNS[FEED_USER_PICTURE_URL_COL_INDEX]+" VARCHAR, "
			+ FEED_COLUMNS[CONTENT_PICTURE_COL_INDEX]	+ " VARCHAR)";

	public static final String CREATE_FRIENDS_TABLE_SQL = "CREATE TABLE "
			+ FRIENDS_TABLE + " (" 
			+ FRIENDS_COLUMNS[ID_COL_INDEX]				+ " INTEGER PRIMARY KEY, " 
			+ FRIENDS_COLUMNS[FRIEND_PICTURE_URL_COL_INDEX]	+ " VARCHAR NOT NULL, "	
			+ FRIENDS_COLUMNS[FRIEND_NAME_COL_INDEX]		+ " VARCHAR NOT NULL)";

	public static final String CREATE_USER_PROFILE_TABLE_SQL = "CREATE TABLE "
			+ USER_PROFILE_TABLE + " (" 
			+ USER_PROFILE_COLUMNS[ID_COL_INDEX]	+ " INTEGER PRIMARY KEY, " 
			+ USER_PROFILE_COLUMNS[NAME_COL_INDEX]		+ " VARCHAR NOT NULL, " 
			+ USER_PROFILE_COLUMNS[USER_NAME_COL_INDEX]	+ " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[BIRTHDAY_COL_INDEX]	+ " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[HOMETOWN_COL_INDEX]	+ " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[LOCATION_COL_INDEX]	+ " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[GENDER_COL_INDEX]	+ " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[RELATIONSHIP_STATUS_COL_INDEX] + " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[RELIGION_COL_INDEX]	+ " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[POLITICAL_COL_INDEX]	+ " VARCHAR, " 
			+ USER_PROFILE_COLUMNS[PICTURE_URL_COL_INDEX]+" VARCHAR)";


	public static final String DROP_FEED_TABLE_SQL = "DROP TABLE IF EXISTS "
			+ FEED_TABLE;

	public static final String DROP_FRIENDS_TABLE_SQL = "DROP TABLE IF EXISTS "
			+ FRIENDS_TABLE;

	public static final String DROP_USER_PROFILE_TABLE_SQL = "DROP TABLE IF EXISTS "
			+ USER_PROFILE_TABLE;

	public DbManager(final Context context, final CursorFactory factory) {
		super(context, DATABASE_NAME, factory, DATABASE_VERSION);

	}

	@Override
	public void onCreate(final SQLiteDatabase db) {
		try {
			db.beginTransaction();
			db.execSQL(CREATE_FEED_TABLE_SQL);
			db.execSQL(CREATE_FRIENDS_TABLE_SQL);
			db.execSQL(CREATE_USER_PROFILE_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
	}

	@Override
	public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
		try {
			db.beginTransaction();
			db.execSQL(DROP_FEED_TABLE_SQL);
			db.execSQL(DROP_FRIENDS_TABLE_SQL);
			db.execSQL(DROP_USER_PROFILE_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		onCreate(db);
	}
	
	public void dropDatabase(final SQLiteDatabase db) {
		try {
			db.beginTransaction();
			db.execSQL(DROP_FEED_TABLE_SQL);
			db.execSQL(DROP_FRIENDS_TABLE_SQL);
			db.execSQL(DROP_USER_PROFILE_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		onCreate(db);
	}

	public Cursor getItems(String marker) {
		return null;
	}

	public long addItem(JSONObject jsonObject) {
		return -1;
	}
	
	public long deleteItem(String itemId) {
		return -1;
	}

	protected ContentValues getContentValues(final JSONObject pItem) {
		return null;
	}
}
